-- @author Dozingfiretruck
-- @license MIT
-- @release 2021.4.29

module(...,package.seeall)

local LCD_WIDTH, LCD_HEIGHT, LCD_BPP = disp.getlcdinfo()

local snake1
local score = 0

local sk = {}
sk.__index = sk
local function snake()
    return setmetatable({
        x = 20+2*20,
        y = 40+0,
        body = {{20+2*20,40+0},{40,40},{20,40}},
        body_len = 3,
        direction = "right",
        food = {},
        run = false,
    }, sk)
end

local function snake_init()
    disp.clear()
    disp.puttext(common.utf8ToGb2312("得分:"),10,2)
    disp.puttext(common.utf8ToGb2312(score),55,2)
    for i = 0, LCD_WIDTH-20, 20 do
        disp.putimage("/lua/wall.png",i,20)
    end
    for i = 0, LCD_WIDTH-20, 20 do
        disp.putimage("/lua/wall.png",i,LCD_WIDTH-20)
    end
    for i = 20, LCD_HEIGHT-20, 20 do
        disp.putimage("/lua/wall.png",0,i)
    end
    for i = 20, LCD_HEIGHT-20, 20 do
        disp.putimage("/lua/wall.png",LCD_HEIGHT-20,i)
    end
    if snake1.direction=="left" then
        disp.putimage("/lua/snake_l.png",snake1.x,snake1.y)
    elseif snake1.direction=="right" then
        disp.putimage("/lua/snake_r.png",snake1.x,snake1.y)
    elseif snake1.direction=="up" then
        disp.putimage("/lua/snake_u.png",snake1.x,snake1.y)
    elseif snake1.direction=="down" then
        disp.putimage("/lua/snake_d.png",snake1.x,snake1.y)
    end
    for k,v in pairs(snake1.body) do
        if k==1 then
            -- body
        else
            disp.putimage("/lua/snake_body.png",v[1],v[2])
        end
    end
    disp.putimage("/lua/start.png",40,100)
    disp.update()
end

function sk:kill()
    self.x=20+2*20
    self.y=40
    self.body = {{20+2*20,40+0},{40,40},{20,40}}
    self.body_len=3
    self.direction="right"
    self.run = false
    score = 0
end

function sk:draw()
    disp.drawrect(20,40,220,220,0x0000)
    if self.direction=="left" then
        self.x = self.x - 20
        disp.putimage("/lua/snake_l.png",self.x,self.y)
    elseif self.direction=="right" then
        self.x = self.x + 20
        disp.putimage("/lua/snake_r.png",self.x,self.y)
    elseif self.direction=="up" then
        self.y = self.y - 20
        disp.putimage("/lua/snake_u.png",self.x,self.y)
    elseif self.direction=="down" then
        self.y = self.y + 20
        disp.putimage("/lua/snake_d.png",self.x,self.y)
    end
    for k,v in pairs(self.body) do
        if self.x == v[1]and self.y == v[2]then
            self.run = false
            break
        end
    end

    if self.x>LCD_WIDTH-20-20 or self.x<20 or self.y>LCD_HEIGHT-20-20 or self.y<20+20 then
        self.run = false
        table.insert(self.body,1,{self.x,self.y})
        table.remove (self.body)
    elseif self.x ==self.food[1] and self.y==self.food[2] then
        disp.drawrect(0,0,240,19,0x0000)
        score=score+1
        disp.puttext(common.utf8ToGb2312("得分:"),10,2)
        disp.puttext(common.utf8ToGb2312(score),55,2)
        table.insert(self.body,1,{self.x,self.y})
        self:putfood()
    else
        table.insert(self.body,1,{self.x,self.y})
        table.remove (self.body)
    end
    for k,v in pairs(self.body) do
        if k == 1 then
            -- body
        else
            disp.putimage("/lua/snake_body.png",v[1],v[2])
        end
    end
end

function sk:putfood()
    local state
    local x
    local y
    repeat
        x = math.random(1,10)*20
        y = math.random(2,10)*20
        state = 0
        for k,v in pairs(self.body) do
            if x == v[1] and y == v[2] then
                state = 1
                break
            end
        end
    until( state == 0 )
    self.food[1]=x
    self.food[2]=y
end

local function game_thread()
    audio.play(0,"FILE","/lua/bgm.mp3",1,nil,true)
    snake1.run = true
    snake1:putfood()
    while true do
        snake1:draw()
        disp.putimage("/lua/food.png",snake1.food[1],snake1.food[2])
        disp.update()
        local result, data = sys.waitUntil("key",500 - score*4)
        if result == true then
            if data == "key_left" then
                snake1.direction="left"
            elseif data == "key_right" then
                snake1.direction="right"
            elseif data == "key_up" then
                snake1.direction="up"
            elseif data == "key_down" then
                snake1.direction="down"
            elseif data == "key_back" then
                snake1.run = false
            elseif data == "key_ok" then
                disp.putimage("/lua/start.png",40,100)
                disp.update()
                while true do
                    local result, data = sys.waitUntil("key",500 - score*4)
                    if result == true then
                        if data == "key_ok" then
                            break
                        end
                    end
                end
            end
        end
        if snake1.run == false then
            snake1:kill()
            snake_init()
            break
        end
    end
end

local function game_init()
    disp.setbkcolor(0x0000)
    disp.clear()
    lcd.setcolor(0x00FF)
    disp.setfontheight(24)
    disp.puttext(common.utf8ToGb2312("贪吃蛇"),(lcd.HEIGHT-string.utf8Len("贪吃蛇")*24)/2,170)
    disp.putimage("/lua/snake.png",90,90)
    disp.update()
    disp.setfontheight(16)
    sys.wait(2000)
    snake1=snake()
    snake_init()
    while true do
        local result, data = sys.waitUntil("key",20)
        if result == true then
            if data == "key_left" then

            elseif data == "key_right" then

            elseif data == "key_up" then

            elseif data == "key_down" then

            elseif data == "key_ok" then
                game_thread()
            end
        end
    end
end

sys.taskInit(game_init)

